package com.helasasa.maxfunds.lineofcredit.cashloan.ghana.screen.tabrepay.subview

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View


class TabReapyGhanaProcessBar(context: Context, attrs: AttributeSet? = null) :
    View(context, attrs) {

    /** 0-1 */
    private var process: Float = 0F

    /** 环宽度 */
    private val lineWidth = 8F.dpToPx()

    private val bgCirclePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        color = Color.parseColor("#FF8C4C")
        strokeWidth = lineWidth
        style = Paint.Style.STROKE
        isAntiAlias = true
        strokeCap = Paint.Cap.ROUND
        strokeJoin = Paint.Join.ROUND
    }

    private val processCirclePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        color = Color.parseColor("#5277D6")
        strokeWidth = lineWidth
        isAntiAlias = true
        style = Paint.Style.STROKE
        strokeCap = Paint.Cap.ROUND
        strokeJoin = Paint.Join.ROUND
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        val widthF = width.toFloat()-lineWidth

        // 画背景环
//        val centerX = width * 1F / 2
//        val centerY = height * 1F / 2
//        canvas.drawCircle(centerX, centerY, centerY, bgCirclePaint);
        canvas.drawArc(
            lineWidth,
            lineWidth,
            widthF,
            widthF,
            0F,
            360F,
            false,
            bgCirclePaint
        )

        // 画进度圆环
        val sweepAngle = 360 * this.process
        canvas.drawArc(
            lineWidth,
            lineWidth,
            widthF,
            widthF,
            -90F,
            sweepAngle,
            false,
            processCirclePaint
        )
    }

    fun updateProcess(curValue: Float) {
        this.process = if (curValue > 1) {
            1F
        } else if (curValue < 0) {
            0F
        } else {
            curValue
        }
        invalidate()
    }

    // 将 dp 转换为像素
    private fun Float.dpToPx(): Float {
        return this * context.resources.displayMetrics.density
    }
}
